# -*- coding: utf-8 -*- 
import datetime
import logging
import urllib2

from google.appengine.ext import db
from BeautifulSoup import BeautifulSoup

from model import *

def update_site_sold_total():
    # create group site
    group_site = get_site()

    # init group_info
    group_infos = list()

    # get groups
    today_groups = group_site.today_infos
    
    for group_info in today_groups:
        update_group_sold_total(group_info)
        

def update_group_sold_total(group_info):
    page = urllib2.urlopen(group_info.link)
    soup = BeautifulSoup(page)

    sold_total = soup.findAll('div', attrs={'id':'deal-status'})[0].findAll('p', attrs={'class':'deal-buy-tip-top'})[0].findAll('strong')[0].getText()

    group_info.sold_total = int(sold_total)
    group_info.put()

    logging.info("groupinfo updated succeed : %s"%group_info.link)
        

def get_site():
    group_site = GroupSite.get_by_key_name("SITE_meituan")
    if group_site is None:
        group_site = GroupSite(key_name="SITE_meituan")
        group_site.code = "meituan"
        group_site.name = "美团".decode('utf8')
        group_site.link = db.Link("http://www.meituan.com/beijing")
        group_site.pop = 60
        group_site.put()
    
    return group_site

def get_data():
    # create group site
    group_site = get_site()
    
    # init group_info
    group_infos = list()
    
    # get groups
    today_groups = group_site.today_infos
    
    if today_groups.count() == 0:
        page = urllib2.urlopen(group_site.link)
        soup = BeautifulSoup(page)

        main_div = soup.findAll('div', attrs={'id' : 'index-deals'})[0]
        items = main_div.findAll('div', attrs={'class' : 'item'})
        items1 = main_div.findAll('div', attrs={'class' : 'item odd'})
        items.extend(items1)
        for item in items:
            title_a = item.findAll('h1')[0].findAll('a')[0]
            title = title_a.getText()
            link = title_a['href']

            picture = item.findAll('img')[0]['src']
            
            current_price = item.findAll('p',attrs={'class' : 'deal-price'})
            current_price = current_price[0].findAll('strong')[0].contents[0][1:]

            table = item.findAll('table',attrs={'class' : 'discount'})
            tr = table[0].findAll("tr")
                
            original_price = tr[1].findAll("td")[0].findAll('strong')[0].contents[0][1:]
            discount = tr[2].findAll("td")[0].findAll('strong')[0].contents[0][:-1]
            
            sold_total = item.findAll('p', attrs={'class':'deal-buy-tip-top'})[0].findAll('strong')[0].contents[0]
        
            group = GroupInfo()
            group.site = group_site
            group.title = title
            group.current_price = float(current_price)
            group.original_price = float(original_price)
            group.discount = float(discount)
            group.picture = db.Link(picture)
            group.link = db.Link(link)
            group.sold_total = int(sold_total)
            group.put()
            
            group_infos.append(group)
    else:
        for group in today_groups:
            group_infos.append(group)
        
    return group_infos


